import BaseScene from "../base/BaseScene";
import Game from "../base/Game";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MainScene extends BaseScene {

    @property(cc.Button)
    private btn_1:cc.Button = null;
    @property(cc.Button)
    private btn_2:cc.Button = null;
    @property(cc.Button)
    private btn_3:cc.Button = null;
    @property(cc.Button)
    private btn_4:cc.Button = null;
    @property(cc.Button)
    private btn_5:cc.Button = null;
    @property(cc.Button)
    private btn_6:cc.Button = null;
    @property(cc.Button)
    private btn_7:cc.Button = null;

    @property(cc.Node)
    private topNode:cc.Node = null;

    /** 按钮的初始位置 */
    private pointArr:Array<number> = [];

    // onLoad(){
    //     cc.warn("2 ==> MainScene");
    // }

    // start(){
    // }

    protected onCreate(): void
    {   
        super.onCreate();
        cc.warn("2 ==> MainScene");
        //Game.init(this);
        cc.warn("游戏初始化...")
       
        console.warn(Game.View);
        
        cc.debug.setDisplayStats(false);
        this.pointArr[0] = this.btn_1.node.x;
        this.pointArr[1] = this.btn_2.node.x;
        this.pointArr[2] = this.btn_3.node.x;
        this.pointArr[3] = this.btn_5.node.y;
        this.pointArr[4] = this.btn_6.node.y;
        this.btn_1.node.on(cc.Node.EventType.TOUCH_END,this.showProgBar,this);
        this.btn_2.node.on(cc.Node.EventType.TOUCH_END,this.showTabView,this);
        this.btn_3.node.on(cc.Node.EventType.TOUCH_END,this.showPagView,this);
        this.btn_4.node.on(cc.Node.EventType.TOUCH_END,this.onClickFunc,this);
        this.btn_5.node.on(cc.Node.EventType.TOUCH_END,this.showBankPanel,this);
        this.btn_6.node.on(cc.Node.EventType.TOUCH_END,this.changeScene,this);
        this.btn_7.node.on(cc.Node.EventType.TOUCH_END,this.showMap,this);
    }

    private showMap():void
    {
        cc.loader.loadRes("prefab/TiledMapPanelPrefab", function (err, prefab) {
            var newNode = cc.instantiate(prefab);
            cc.director.getScene().addChild(newNode);
        });
    }

    private changeScene():void
    {
        cc.director.loadScene('BattleScene');
    }

    private showBankPanel():void
    {
        cc.loader.loadRes("prefab/BankPanelPrefab", function (err, prefab) {
            var newNode = cc.instantiate(prefab);
            cc.director.getScene().addChild(newNode);
        });
    }

    private showPagView():void
    {
        cc.loader.loadRes("prefab/PagViewPanelPrefab",function (err, prefab){
            var pagView = cc.instantiate(prefab);
            cc.director.getScene().addChild(pagView);
        })
    }

    private showTabView():void
    {
        cc.loader.loadRes("prefab/TabViewPanelPrefab",function(err,prefab){
            var tabView = cc.instantiate(prefab);
            cc.director.getScene().addChild(tabView);
        })
    }

    private showProgBar():void
    {
        cc.loader.loadRes("prefab/ProgressBarPanelPrefab",function(err,prefab){
            var progBar = cc.instantiate(prefab);
            cc.director.getScene().addChild(progBar);
        })
    }

    private btnState:number = 0;
    private onClickFunc():void
    {
        if(this.btnState == 0)
        {
            this.ccTweenFunc(this.btn_1,this.btn_4.node.x,0);
            this.ccTweenFunc(this.btn_2,this.btn_4.node.x,0);
            this.ccTweenFunc(this.btn_3,this.btn_4.node.x,0);
            this.ccTweenFunc(this.btn_5,0,this.btn_4.node.y);
            this.ccTweenFunc(this.btn_6,0,this.btn_4.node.y);
            this.btnState = 1;

            cc.loader.loadRes("main/icon/main_btns_reback_btn_up", cc.SpriteFrame, (err, spriteFrame)=>{
                this.btn_4.normalSprite = spriteFrame;
            });
         
        }
        else if(this.btnState == 1)
        {
            this.ccTweenFunc(this.btn_1,this.pointArr[0],0);
            this.ccTweenFunc(this.btn_2,this.pointArr[1],0);
            this.ccTweenFunc(this.btn_3,this.pointArr[2],0);
            this.ccTweenFunc(this.btn_5,0,this.pointArr[3]);
            this.ccTweenFunc(this.btn_6,0,this.pointArr[4]);
            this.btnState = 0;

            cc.loader.loadRes("main/icon/main_btns_more_btn_up", cc.SpriteFrame, (err, spriteFrame)=>{
                this.btn_4.normalSprite = spriteFrame;
            });
        }
    }

    private ccTweenFunc(_target:any,_x:number,_y:number):void
    {   
        if(_x == 0)
        {
            cc.tween(_target.node)
                .to(0.2, { y: _y},{ easing: 'sineOut'})
                .start()
        }
        else if(_y == 0)
        {
            cc.tween(_target.node)
                .to(0.2, { x: _x},{ easing: 'sineOut'})
                .start()
        }
    }

    // update(dt){

    // }

    // lateUpdate(){

    // }

    // onDestroy(){

    // }

    // protected onDestroy()
    // {   
    //     // this.btn_4.node.off(cc.Node.EventType.TOUCH_END,this.onClickFunc,this);
    //     // this.btn_5.node.off(cc.Node.EventType.TOUCH_END,this.showBankPanel,this);
    //     // this.btn_6.node.off(cc.Node.EventType.TOUCH_END,this.changeScene,this);
    //     super.onDestroy();
    // }

    // onEnable(){

    // }

    // onDisable(){

    // }
}
